//
//  AddToCartView.m
//  WeiYiMei
//
//  Created by 李叶夫 on 2019/4/16.
//  Copyright © 2019年 liyefu. All rights reserved.
//

#import "AddToCartView.h"
#import "ANGradientButton.h"

@interface AddToCartView ()

@property (nonatomic, strong) ANGradientButton *bgView;
@property (nonatomic, strong) UIButton *buyButton;
@property (nonatomic, strong) UIButton *addToCartButton;
@property (nonatomic, strong) UIView *sep;
@property (nonatomic, strong) UIImageView *contactIcon;
@property (nonatomic, strong) UILabel *contactLab;
@property (nonatomic, strong) UIButton *contactButton;

@end

@implementation AddToCartView

- (void)contactAction:(id)sender
{
    if (self.contactHandler) {
        self.contactHandler();
    }
}

- (void)buyAction:(id)sender
{
    if (self.buyHandler) {
        self.buyHandler();
    }
}

- (void)addCartAction:(id)sender
{
    if (self.addCartHandler) {
        self.addCartHandler();
    }
}

- (void)setupSubViews
{
    self.bgView = [[ANGradientButton alloc] initWithFrame:CGRectMake(0, 0, 170, 32)];
    [self addSubview:self.bgView];
    self.bgView.layer.cornerRadius = 16;
    self.bgView.layer.masksToBounds = YES;
    
    self.buyButton = [[ANGradientButton alloc] init];
    self.buyButton.backgroundColor = [UIColor clearColor];
    [self.buyButton setTitle:@"立即购买" forState:UIControlStateNormal];
    [self.buyButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.buyButton.titleLabel.font = [UIFont systemFontOfSize:14];
    [self.bgView addSubview:self.buyButton];
    
    self.addToCartButton = [[ANGradientButton alloc] init];
    self.addToCartButton.backgroundColor = [UIColor clearColor];
    [self.addToCartButton setTitle:@"加购物车" forState:UIControlStateNormal];
    [self.addToCartButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.addToCartButton.titleLabel.font = [UIFont systemFontOfSize:14];
    [self.bgView addSubview:self.addToCartButton];
    
    self.sep = [[UIView alloc] init];
    self.sep.backgroundColor = UIColorFromRGB(0x9ECAFF);
    [self.bgView addSubview:self.sep];
    
    self.contactLab = [[UILabel alloc] init];
    self.contactLab.textColor = kColor333333;
    self.contactLab.font = [UIFont systemFontOfSize:12];
    self.contactLab.text = @"联系商家";
    [self addSubview:self.contactLab];
    
    self.contactIcon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"sd_contect"]];
    [self addSubview:self.contactIcon];
    
    self.contactButton = [[UIButton alloc] init];
    self.contactButton.backgroundColor = [UIColor clearColor];
    [self addSubview:self.contactButton];
    
    [self.contactButton addTarget:self action:@selector(contactAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.buyButton addTarget:self action:@selector(buyAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.addToCartButton addTarget:self action:@selector(addCartAction:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)setupConstraints
{
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(12);
        make.right.equalTo(self).offset(-16);
        make.width.equalTo(@170);
        make.height.equalTo(@32);
    }];
    
    [self.sep mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.bgView);
        make.top.bottom.equalTo(self.bgView);
        make.width.equalTo(@1);
    }];
    
    [self.buyButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.top.bottom.equalTo(self.bgView);
        make.left.equalTo(self.sep.mas_right);
    }];
    
    [self.addToCartButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.bottom.equalTo(self.bgView);
        make.right.equalTo(self.sep.mas_left);
    }];
    
    [self.contactIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(8);
        make.left.equalTo(self).offset(28);
    }];
    
    [self.contactLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.contactIcon);
        make.top.equalTo(self.contactIcon.mas_bottom).offset(5);
    }];
    
    [self.contactButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.contactLab);
        make.top.equalTo(self);
        make.height.equalTo(@55);
    }];
}

@end
